package com.zwr.normal.sharemodel.aba;

import java.util.concurrent.atomic.AtomicMarkableReference;

public class MarkAbleReferenceDemo {
    public static void main(String[] args) throws InterruptedException {
        GarbageBag bag = new GarbageBag("垃圾袋满了");
        AtomicMarkableReference<GarbageBag> ref = new AtomicMarkableReference<>(bag, true);
        GarbageBag pre = ref.getReference();

        new Thread(() -> {
            System.out.println("打扫卫生的线程 start...");
            bag.setDesc("空垃圾袋");
            while (!ref.compareAndSet(bag, bag, true, false)) {}
        }).start();
        Thread.sleep(1000);
        System.out.println("主线程想换一只新垃圾袋？");
        boolean success = ref.compareAndSet(pre, new GarbageBag("空垃圾袋"), true, false);
        System.out.println("换了么？" + success);

    }
}
